
/*******************************************************************************
** CopyRight (c) 2005-2008 SKY-MOBI Ltd. All rights reserved.
** Filename:  helpWin.c
** Creator:   yuzhifeng
** Version:   0.0
** Date:      
** Description:
 *******************************************************************************/

#include "commdef.h"
#include "mpsFuncPool.h"

#include "appmgr_i.h"
#include "sgl_window.h"
#include "smp.h"
#include "smp_list.h"
#include "smp_textinfo.h"
#include "smp_toolbar.h"
#include "smp_msgbox.h"
#include "smp_menuwnd.h"
 
#include "string.h"
#include "res.h"
#ifndef SDK_NO_PLUGIN
#include "sgl_string.h"
#include "appmgr_string.h"
#else
#include "cookie_string.h"
#endif
#include "appmgr_guimain.h"
#include "appmgr_config.h"
#include "rollbar.h"

#include "sysfunc.h"

#define LIST_HAS_BOTTOM_AD     0

enum {
	HELPWIN_LIST = 1,
	HELPWIN_CONTENT,
	HELPTABWIN_LIST
};

typedef struct _toplist_data_
{
	mpsFpFuncType countFunc;
	mpsFpFuncType rowFunc;
	mpsFpFuncType handleFunc;
	int32 titleId;
	int32 bmpId;
} TOPLIST_DATA;

typedef struct _topTxt_data_
{
	PCWSTR content;
	int32 titleId;
	int32 bmpId;
} TOPTXT_DATA;


static int32 sFunc_Count, sFunc_Row;

static int32 GetHelpListCount(int32 hList, int32 p1, int32 p2, int32 p3, int32 p4, int32 p5)
{
	return (STR_HELP_CONTACT - STR_HELP_ABOUT + 1);
}

static int32 GetHelpListInfo(int32 hList, int32 index, int32 row, int32 p3, int32 p4, int32 p5)
{
	PSMPROWDATA pRowData = (PSMPROWDATA)row;
	
	pRowData->hBmp.pData = SGL_LoadBitmap(BMP_HELP_ABOUT+index, &pRowData->hBmp.width, &pRowData->hBmp.height);
	pRowData->cols[0].str = (PCWSTR)SGL_LoadString(index+STR_HELP_ABOUT);
	pRowData->cols[0].width = (uint16)_WIDTH((HWND)hList) - 2*SMP_ITEM_MARGIN - pRowData->hBmp.width;
	pRowData->colWidth0 = pRowData->hBmp.width;
	pRowData->margin = 3;
	return MR_SUCCESS;
}

#define STR_BUF2_LEN 640
static uint8 strBuf2[STR_BUF2_LEN];
static int32 ShowHelpContent(int32 index, int32 p1, int32 p2, int32 p3, int32 p4, int32 p5)
{
	uint8* str = CFG_GetStr(index);
	if(str == NULL)
		str = SGL_LoadString(index+STR_HELP_ABOUT_C);
#ifndef SDK_NO_PLUGIN
	if(0 == index)
	{
		int32 mainVer = 0, appVer = 0, sglVer = 0, acntVer = 0;
		int32 stat;
		uint8 ascBuf[100];
		mainVer = GAL_GetMainVersion();
		mpsFpGetExtInfo(EXT_ID_APPMGR, &appVer, &stat);
		mpsFpGetExtInfo(EXT_ID_SGL, &sglVer, &stat);
#ifdef WITH_APPSTORE
		mpsFpGetExtInfo(EXT_ID_ACCOUNT, &acntVer, &stat);
		sprintf((char *)ascBuf, "Main:V%d.%d\nAppmgr:V%d.%d\nGui:V%d.%d\nAccount:V%d.%d\n", DIV(mainVer, 1000), MOD(mainVer, 1000),
			DIV(appVer, 1000), MOD(appVer, 1000),DIV(sglVer, 1000), MOD(sglVer, 1000),DIV(acntVer, 1000), MOD(acntVer, 1000));
#else
		sprintf((char *)ascBuf, "Main:V%d.%d\nAppmgr:V%d.%d\nGui:V%d.%d\n", DIV(mainVer, 1000), MOD(mainVer, 1000),
			DIV(appVer, 1000), MOD(appVer, 1000),DIV(sglVer, 1000), MOD(sglVer, 1000));
#endif
		wstrcpy((PWSTR)strBuf2, (PCWSTR)str);
		str2wstr((PCSTR)ascBuf, (PWSTR)(strBuf2+wstrlen((PCWSTR)strBuf2)), STR_BUF2_LEN-wstrlen((PCWSTR)strBuf2));
		str = strBuf2;
	}
#else
	if(0 == index)
	{
		int32 mainVer = 0;
		uint8 ascBuf[100]={0};
		mainVer = GAL_GetMainVersion();
		sprintf((char *)ascBuf, "Main_NoPlugin:V%d.%d\n", DIV(mainVer, 1000), MOD(mainVer, 1000));
		wstrcpy((PWSTR)strBuf2, (PCWSTR)str);
		str2wstr((PCSTR)ascBuf, (PWSTR)(strBuf2+wstrlen((PCWSTR)strBuf2)), STR_BUF2_LEN-wstrlen((PCWSTR)strBuf2));
		str = strBuf2;
	}
#endif
	AppMgr_ShowTopTextWin(str, index+STR_HELP_ABOUT, BMP_SUB_TITLE);
	return MR_SUCCESS;
}

LRESULT HelpIndex_WinProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{

	HWND hControl;
	TOPLIST_DATA* pData = (TOPLIST_DATA*)_USERDATA(hWnd);
	switch(Msg)
	{

	case WM_CREATE:
	{
		if(!pData) return 1;
		
		hControl = SMP_CreateWindow(SMP_LIST_WNDPROC, 
			0, SMP_TITLE_HEIGHT, _WIDTH(hWnd), _HEIGHT(hWnd)-SMP_TITLE_HEIGHT-SMP_TOOLBAR_HEIGHT,
			HELPWIN_LIST, SMP_LISTS_LIST_MODE|WS_TABSTOP|SMP_LISTS_CIRCULAR, 0);
#ifndef SDK_NO_PLUGIN
		sFunc_Count = mpsFpSetCallBackFunc(MODULE_ID, pData->countFunc);
		sFunc_Row = mpsFpSetCallBackFunc(MODULE_ID, pData->rowFunc);
		SMP_List_SetDataProvider(hControl, sFunc_Count, sFunc_Row);
#else
		SMP_List_SetDataProvider(hControl, pData->countFunc, pData->rowFunc);
#endif
		SGL_AddChildWindow(hWnd, hControl);
		
		SMP_CreateToolbar(hWnd, STR_OK, STR_BACK);
		break;
	}
	case WM_DESTROY:
	{
#ifndef SDK_NO_PLUGIN
		mpsFpUnsetCallBackFuncByCode(sFunc_Count);
		mpsFpUnsetCallBackFuncByCode(sFunc_Row);
#endif
		if(pData)
			SGL_FREE(pData);
		break;
	}
	case WM_INITFOCUS:
	{
		SGL_FocusNext(hWnd, 1);
		break;
	}
	case WM_SHOW:
	{
#if LIST_HAS_BOTTOM_AD
		//AD_InsertAdWin(hWnd);
#endif
	}
	case WM_PAINT:
	{
		bmpSt Icon;
		Icon.pData = SGL_LoadBitmap(pData->bmpId, &Icon.width, &Icon.height);
		SMP_DrawTopWinHeader((DWORD)&Icon, (PCWSTR)SGL_LoadString(pData->titleId));
		break;
	}
	case WM_KEYUP:
	{
		switch(wParam)
		{
		case MR_KEY_SOFTLEFT:
		{
			hControl = SGL_FindChildWindow(hWnd, HELPWIN_LIST);
			if(hControl) {
				int32 index;
				index = SMP_List_GetHilightIndex(hControl);
				if(pData->handleFunc)
					pData->handleFunc(index, 0, 0, 0, 0, 0);
			}
			return 1;
		}
		case MR_KEY_SOFTRIGHT:
		{
			ALU_HideTopWindow(TOPWIN_HELPINDEX, TRUE, TRUE);
			return 1;
		}
		}
		break;
	}
	case WM_COMMAND:
	{
		WID id = LOWORD(wParam);
		WORD code = HIWORD(wParam);
		if(id == HELPWIN_LIST)
		{
			switch(code)
			{
			case SMP_LISTN_SELECTED:
			case SMP_LISTN_CLICKED:
			{
				if(pData->handleFunc)
					pData->handleFunc(lParam, 0, 0, 0, 0, 0);
				break;
			}
#if LIST_HAS_BOTTOM_AD
			case SMP_LISTN_SCROLL_OUT:
			{
				if(lParam == SMP_LIST_OUT_OF_BOTTOM)
				{
					SGL_FocusNext(hWnd, 1);
				}
				else if(lParam == SMP_LIST_OUT_OF_TOP)
				{
					SGL_FocusNext(hWnd, 0);
				}
				break;
			}
#endif
			}
		}
		return 0;
	}

	}
	return 0;
   }


LRESULT HelpContent_WinProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{

	HWND hControl;
	TOPTXT_DATA* pData = (TOPTXT_DATA*)_USERDATA(hWnd);

	switch(Msg)
	{

	case WM_CREATE:
	{
		if(!pData) return 1;
		
		hControl = SMP_CreateWindow(SMP_TEXTINFO_WNDPROC, 
			0, SMP_TITLE_HEIGHT, _WIDTH(hWnd), _HEIGHT(hWnd)-SMP_TITLE_HEIGHT-SMP_TOOLBAR_HEIGHT,
			HELPWIN_CONTENT, WS_TABSTOP, 0);
		SMP_TextInfo_SetContent(hControl, pData->content);
		SGL_AddChildWindow(hWnd, hControl);
		
		if(STR_HELP_CONTACT == pData->titleId)
			SMP_CreateToolbar(hWnd, STR_CALL_CS, STR_BACK);
		else
			SMP_CreateToolbar(hWnd, -1, STR_BACK);
		return 0;
	}
	case WM_DESTROY:
	{
		break;
	}
	case WM_INITFOCUS:
	{
		SGL_FocusNext(hWnd, 1);
		break;
	}
	case WM_SHOW:
	{
		break;
	}
	case WM_PAINT:
	{
		bmpSt Icon;
		
		Icon.pData = SGL_LoadBitmap(pData->bmpId, &Icon.width, &Icon.height);
		SMP_DrawTopWinHeader((DWORD)&Icon, (PCWSTR)SGL_LoadString(pData->titleId));
		break;
	}
	case WM_KEYDOWN:
	case WM_KEYDOWNREPEAT:
	{
		break;
	}
	case WM_KEYUP:
	{
		switch(wParam) 
		{
		case MR_KEY_SOFTRIGHT:
		{
			ALU_HideTopWindow(TOPWIN_HELPCONTENT, TRUE, TRUE);
			return 1;
		}
		case MR_KEY_SOFTLEFT:
		{
			if(STR_HELP_CONTACT == pData->titleId)
			{
				if(MR_NET_ID_NONE != gNetStat)
				{
					mrc_call("4006869028");
				}
				else {
					ALU_showCommMsgBox(MSGBOX_COMM_NOTI, STR_NO_SIM_ERR, ID_OK|SMP_MSGBOXS_AUTOCLOSE);
				}
				return 1;
			}
		}
		}
		break;
	}

	}
	return 0;
   }

int32 AppMgr_ShowHelpWin(void)
{
	AppMgr_ShowTopListWin(GetHelpListCount, GetHelpListInfo, ShowHelpContent, STR_MENU_HELP, BMP_SUB_TITLE);
	return MR_SUCCESS;
}

int32 AppMgr_ShowTopListWin(mpsFpFuncType countFunc, mpsFpFuncType rowFunc, mpsFpFuncType handleFunc, int32 titleId, int32 bmpId)
{
	TOPLIST_DATA* pData = SGL_MALLOC(sizeof(TOPLIST_DATA));
	if(pData)
	{
		pData->countFunc = countFunc;
		pData->rowFunc = rowFunc;
		pData->handleFunc = handleFunc;
		pData->titleId = titleId;
		pData->bmpId = bmpId;

		ALU_ShowTopWindow(TOPWIN_HELPINDEX, NULL, (int32)pData);
	}
	return MR_SUCCESS;
}

int32 AppMgr_ShowTopTextWin(PCWSTR content, int32 titleId, int32 bmpId)
{
	TOPTXT_DATA* pData = SGL_MALLOC(sizeof(TOPTXT_DATA));
	if(pData)
	{
		pData->content = content;
		pData->titleId = titleId;
		pData->bmpId = bmpId;

		ALU_ShowTopWindow(TOPWIN_HELPCONTENT, NULL, (int32)pData);
	}
	return MR_SUCCESS;
}


LRESULT Help_WinProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{

	static int32 sFunc_Count,sFunc_Row;
	HWND hControl = SGL_FindChildWindow(hWnd, HELPTABWIN_LIST);
	switch(Msg)
	{

	case WM_CREATE:
	{
		int16 w=0,h=0;
		List_InsertGwpHeightWidth(hWnd,&h,&w);
		hControl = SMP_CreateWindow(SMP_LIST_WNDPROC, 
#if LIST_HAS_BOTTOM_AD
			//0, 0, _WIDTH(hWnd), _HEIGHT(hWnd)-SMP_TOOLBAR_HEIGHT-ADWIN_HEIGHT,
			0, 0, _WIDTH(hWnd), _HEIGHT(hWnd)-SMP_TOOLBAR_HEIGHT-h,
#else
			0, 0, _WIDTH(hWnd), _HEIGHT(hWnd)-SMP_TOOLBAR_HEIGHT,
#endif
			HELPTABWIN_LIST, SMP_LISTS_LIST_MODE|WS_TABSTOP, 0);
#ifndef SDK_NO_PLUGIN
		sFunc_Count = mpsFpSetCallBackFunc(MODULE_ID,GetHelpListCount);
		sFunc_Row = mpsFpSetCallBackFunc(MODULE_ID, GetHelpListInfo);
		SMP_List_SetDataProvider(hControl, sFunc_Count, sFunc_Row);
#else
		SMP_List_SetDataProvider(hControl, GetHelpListCount, GetHelpListInfo);
#endif
		SGL_AddChildWindow(hWnd, hControl);
		
#if LIST_HAS_BOTTOM_AD
		List_InsertGwpWin(hWnd);
#endif
		SMP_CreateToolbar(hWnd, STR_OK, STR_SWITCH);
		_SET_STYLE(hWnd, WS_TRANSPARENT);
		return 0;
	}
	case WM_DESTROY:
	{
#ifndef SDK_NO_PLUGIN
		mpsFpUnsetCallBackFuncByCode(sFunc_Count);
		mpsFpUnsetCallBackFuncByCode(sFunc_Row);
#endif
		return 0;
	}
	case WM_INITFOCUS:
	{
		SGL_FocusNext(hWnd, 1);
		return 0;
	}
	case WM_SHOW:
	{	
	//	SGL_DEBUG("mem top Show UserCenter win!");
		hControl = SGL_FindChildWindow(hWnd, HELPTABWIN_LIST);
		SGL_SetFocusWindow(SGL_GetTopWindow(hWnd), hControl);
		rollBar_Create(hWnd);
		return 0;
	}
	case WM_PAINT:
	{
		HWND hToolbar = SGL_FindChildWindow(hWnd, COMM_WID_TOOLBAR);

		hControl = SGL_FindChildWindow(hWnd, HELPTABWIN_LIST);
		if(_IS_SET_ALL(hControl, WS_FOCUSED))
		{
			SMP_Toolbar_SetStrings(hToolbar, STR_OK, -1, STR_SWITCH, 0);
		}
		else {
			SMP_Toolbar_SetStrings(hToolbar, -1, -1, STR_SWITCH, 0);
		}
		return 0;
	}
	case WM_KEYUP:
	{
		switch(wParam)
		{
		case MR_KEY_SOFTLEFT:
		{
			hControl = SGL_FindChildWindow(hWnd, HELPTABWIN_LIST);
			if(hControl&&_IS_SET_ALL(hControl, WS_FOCUSED)) {
				int32 index;
				index = SMP_List_GetHilightIndex(hControl);
				ShowHelpContent(index, 0, 0, 0, 0, 0);
			}
			return 1;
		}
		}
		return 0;
	}
	case WM_TABWND_FOCUSTABBAR:
	{
		return 0;
	}
	case WM_TABWND_FOCUSFRAME:
	{
		HWND hList = SGL_FindChildWindow(hWnd, HELPTABWIN_LIST);
		if(MR_KEY_UP == wParam)
		{
			SMP_List_HilightByIndex(hList, GetHelpListCount((int32)hList, 0, 0, 0, 0, 0)-1);
		}
		else if(MR_KEY_DOWN == wParam)
		{
			SMP_List_HilightByIndex(hList, 0);
		}
		return 0;
	}
	case WM_COMMAND:
	{
		WID id = LOWORD(wParam);
		WORD code = HIWORD(wParam);
		if(id == HELPTABWIN_LIST)
		{
			switch(code)
			{
				case SMP_LISTN_SELECTED:
				case SMP_LISTN_CLICKED:
				{
					ShowHelpContent(lParam, 0, 0, 0, 0, 0);
					break;
				}
				case SMP_LISTN_SCROLL_OUT:
				{
					Msg = WM_KEYDOWN;
					if(SMP_LIST_OUT_OF_BOTTOM == lParam)
					{
						wParam = MR_KEY_DOWN;
					}
					else if(SMP_LIST_OUT_OF_TOP == lParam)
					{
						wParam = MR_KEY_UP;
					}
					SGL_SendMessage(_PARENT(hWnd), Msg, wParam, 0); 
					break;
				}
				case SMP_LISTN_MOUSEUP:
				{
					SGL_UpdateWindow(hWnd);
				}

			}
		}
		return 0;
	}

	}
	return  SMP_MenuWnd_WndProc(hWnd, Msg, wParam, lParam);
   }

